package com.occludens.ui;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.google.api.server.spi.auth.common.User;
import com.occludens.admin.datamodel.Role;
import com.occludens.admin.ui.Command;
import com.occludens.admin.ui.Menu;
import com.occludens.data.admin.util.AdminUtil;
import com.occludens.util.XMLUtil;

public class UINavigationToolbar {
	
	public String getNavigationToolbar(String menuName) throws Exception {
		try {
			String returnString = "";
			AdminUtil.register();
			//Get navigation menu
			
			Menu appNavigationToolbarMenu = Menu.getMenu(menuName);
			if (appNavigationToolbarMenu!=null) {
				returnString = getMenuXMLStructure(appNavigationToolbarMenu);
			}						
			return returnString;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	private String getMenuXMLStructure(Menu menu) throws Exception {
		try {
			String returnString = "";
			
			if (menu!=null) {
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				Document document = builder.newDocument();

				Element rootElement = document.createElement("root");
				document.appendChild(rootElement);
				getMenuElement(document, rootElement, menu);
				returnString = XMLUtil.getXMLString(document);
				System.out.println("returnString = " + returnString);
			}
			return returnString;
		} catch (Exception e) {
			throw e;
		}
	}
	
	private static void getMenuElement(Document document, Element element, Menu menu) throws Exception {
		try {
			Element menuElement = null;
			if (menu!=null) {
				menuElement = document.createElement("menu");
				menuElement.setAttribute("name", menu.getName());
				menuElement.setAttribute("description", menu.getDescription());
				menuElement.setAttribute("label", menu.getLabel());
				menuElement.setAttribute("link", menu.getLink());
				
				Map<String,String> settings = menu.getSettings();
				Iterator<String> settingsKeySetItr = settings.keySet().iterator();
				while (settingsKeySetItr.hasNext()) {
					String settingKey = settingsKeySetItr.next();
					String settingValue = settings.get(settingKey);
					if (StringUtils.isNotBlank(settingKey) && StringUtils.isNotBlank(settingValue)) {
						Element settingsElement = document.createElement("settings");
						settingsElement.setAttribute("name", settingKey);
						settingsElement.setAttribute("value", settingValue);
						menuElement.appendChild(settingsElement);
					}
				}//End of while settingsKeySetItr
				element.appendChild(menuElement);
				
				List<Object> items = menu.getItems();
				Iterator<Object> itemsItr = items.iterator();
				while (itemsItr.hasNext()) {
					Object item = itemsItr.next();
					if (item.getClass().equals(Menu.class)) {
						getMenuElement(document, menuElement, (Menu)item);
					} else if (item.getClass().equals(Command.class)) {
						getCommandElement(document, menuElement, (Command)item);
					}
				}//End of while itemsItr
			}
		} catch (Exception e) {
			throw e;
		}
	}
	
	private static void getCommandElement(Document document, Element element, Command command) throws Exception {
		try {
			Element commandElement = null;
			if (command!=null) {
				commandElement = document.createElement("command");
				commandElement.setAttribute("name", command.getName());
				commandElement.setAttribute("description", command.getDescription());
				commandElement.setAttribute("label", command.getLabel());
				commandElement.setAttribute("link", command.getLink());
				
				Map<String,String> settings = command.getSettings();
				Iterator<String> settingsKeySetItr = settings.keySet().iterator();
				while (settingsKeySetItr.hasNext()) {
					String settingKey = settingsKeySetItr.next();
					String settingValue = settings.get(settingKey);
					if (StringUtils.isNotBlank(settingKey) && StringUtils.isNotBlank(settingValue)) {
						Element settingElement = document.createElement("setting");
						settingElement.setAttribute("name", settingKey);
						settingElement.setAttribute("value", settingValue);
						commandElement.appendChild(settingElement);
					}
				}//End of while settingsKeySetItr
				
				List<Object> accesses = command.getAccess();
				Iterator<Object> accessesItr = accesses.iterator();
				while (accessesItr.hasNext()) {
					Object access = accessesItr.next();
					if (access!=null) {
						Element accessElement = document.createElement("access");
						if (access.getClass().equals(User.class)) {
							accessElement.setAttribute("type", "user");
							///TODO
							//accessElement.setAttribute("name", ((User)access).getName());
						} else if (access.getClass().equals(Role.class)) {
							accessElement.setAttribute("type", "role");
							accessElement.setAttribute("name", ((Role)access).getName());
						}
					}
				}//End of while accessesItr
				element.appendChild(commandElement);
			}
		} catch (Exception e) {
			throw e;
		}
	}

}
